package com.jingzhi.huimiao;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.util.Log;
import android.widget.ImageView;

import com.jingzhi.huimiao.utils.DataStoreUtil;
import com.jingzhi.huimiao.utils.SQLdm;

/**
 * 关卡结束页面
 * 
 * @author cxk
 * 
 * 
 */
public class EndTestActivity extends Activity {

	private Long taskid;
	private ArrayList<Long> tasklist;
	private SQLiteDatabase db;
	private int rightcount;
	
	private ImageView iv_star1;
	private ImageView iv_star2;
	private ImageView iv_star3;
	private Cursor rawQuery;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_end_test);

		Long chapterid = DataStoreUtil.getLong(EndTestActivity.this, "CHOOSRCHAPTER");
		Log.e("chapterid", chapterid+"");
		
		initview();
		
		// 打开数据库输出流
		SQLdm s = new SQLdm();
		db = s.openDatabase(getApplicationContext());

		// 查询数据库中testid=1的数据

		tasklist = new ArrayList<Long>();

		Log.e("chapterid------", chapterid+"");
		
		// 查询小关中的task 返回值 是 task集合
				String sql = "select * from unit where pid=?;";
				Cursor cursor = db.rawQuery(sql,
						new String[] { chapterid.toString() });
				while (cursor.moveToNext()) {
					long taskid = cursor.getLong(cursor.getColumnIndex("id"));
					Log.e("taskid----", taskid + "");
					tasklist.add(taskid);
				}
		
		rightcount = 0;
		for(int i=0;i<tasklist.size();i++) {
			rawQuery = db.rawQuery("select id from word where pid ='"+tasklist.get(i)+"' and word_right=2;", null);
			Log.e("rightcount-----fs-", tasklist.get(i).toString());
			rightcount += rawQuery.getColumnCount();
			Log.e("rightcount-----fsfdsfsdf-", rawQuery.getCount()+"");
		}
		
		//求得正确率
		float accuracy = (float) (rightcount/84.0);
		
		if(accuracy == 1){
			iv_star3.setBackgroundResource(R.drawable.icon_save1);
		}else if(accuracy >0.2){
			iv_star2.setBackgroundResource(R.drawable.icon_save1);
		}else if(accuracy >0){
			iv_star1.setBackgroundResource(R.drawable.icon_save1);
		}
	}

	private void initview() {
		iv_star1 = (ImageView) findViewById(R.id.iv_star1);
		iv_star2 = (ImageView) findViewById(R.id.iv_star2);
		iv_star3 = (ImageView) findViewById(R.id.iv_star3);
	}
}
